﻿<?php

class Category extends BaseModel
{
      var $table  = 'category';	

      var $V_type  = '';	

     function show($type='')
     {  
	 
	     if($type)
		 {
		  $this->V_type = $type;
		 } 
		 		  
	     if($this->V_type)
		 {
		  $conditions = "type='{$this->V_type}'";
		 } 
		   
	 
 		 $info = $this->S_Sql_Exe(array('fields' =>'*','conditions' =>"{$conditions}",'order' =>" sort_order asc,cat_id asc"));   
		 return $info;
        
     }

    /* 插入数据 */
    function post($data)
    {    
	
  	 $this->IU_Sql_Exe(array ('data' => array('cat_name'=>"$data[cat_name]",'pinyin'=>"$data[pinyin]",'parent_id'=>"$data[parent_id]",'sort_order'=>"$data[sort_order]",'type'=>"$data[type]",'viewname'=>"$data[viewname]",'viewshowname'=>"$data[viewshowname]"), 'mode' => 'INSERT'));

		 return $this->db->insert_id();        
    }


    /* 通过id取出一条数据 */
    function oneshow($id ='')
    {    
	      
		 $info = $this->S_Sql_Exe(array('fields' =>'*','conditions' =>"cat_id='{$id}'",'return' =>'one'));
    
		 return $info;
        
    }


    /* 修改数据 */
    function editaction($id ='',$data = '')
    {    
      
		return $this->IU_Sql_Exe(array ('data' => array('cat_name'=>"$data[cat_name]",'pinyin'=>"$data[pinyin]",'parent_id'=>"$data[parent_id]",'sort_order'=>"$data[sort_order]",'type'=>"$data[type]",'viewname'=>"$data[viewname]",'viewshowname'=>"$data[viewshowname]"),'mode' => 'UPDATE','conditions' =>"cat_id = '{$id}' "));
        
    }

    /* 修改数据 */
    function editsort($id ='',$data = '')
    {   
     
		return $this->IU_Sql_Exe(array ('data' => array('sort_order'=>"$data[sort_order]"),'mode' => 'UPDATE','conditions' =>"cat_id = '{$id}' "));
        
    }

    /* 删除数据 */
    function del($id ='')
    {      
		return $this->D_Sql_Exe(array ('conditions' =>"cat_id = '{$id}' "));
        
    }

	
      function getnbsp($i) 
	  {
	        $str = '';
	        if($i) 
			{
		         for($j=0; $j<$i; $j++)
				 {
			      $str .= "　";
		         }
	        }
	        return $str;
     }


      function write_cache($type='') 
	  {
	     if($type)
		 {
		  $this->V_type = $type;
		 }
		 
		$data = $this->get_list_category($this->show($this->V_type));
		if(empty($data))$data=array();  		 		  
		$cache_filename = ROOT_PATH . '/data/category/category_'. $this->V_type .'.php';
		
	    write_file($cache_filename, "<?php \nreturn " . var_export($data , true) . ";\n?>");
		
		if(!is_writable($cache_filename))
		{
		exit('file directory is not writed power');//文件目录权限不可写
	    }
		
     }



     function get_list_category($array,$parentid=0,$level=0,$add=2) 
	 {
	        $str_pad = $this->getnbsp($level);
	        $newarray = array();
	        $temp = array();
	        foreach($array as $v) 
			{
		       if($v['parent_id'] == $parentid) 
			   {
			       $newarray[] = array(
				         'cat_id' => $v['cat_id'],
				         'cat_name' => $v['cat_name'],
				         'pinyin' => $v['pinyin'],						 
				         'parent_id' => $v['parent_id'],
				         'sort_order' => $v['sort_order'],
				         'viewname' => $v['viewname'],
				         'viewshowname' => $v['viewshowname'],
				         'str_pad' => $str_pad
			                          );
			       $temp = $this->get_list_category($array,$v['cat_id'],($level+$add));
			       if($temp) 
				   {
				          $newarray = array_merge($newarray, $temp);
			       }
		      }
	       }
	       return $newarray;
    }


     function get_list_id_category($array,$parentid=0,$str='') 
	 {
	        foreach($array as $v) 
			{
		       if($v['parent_id'] == $parentid) 
			   {
				   $str .=$v['cat_id'].',';				  
			       $str .= $this->get_list_id_category($array,$v['cat_id'],$str);
		      }
	       }
	       return $str;
    }
/* 该函数主要是用来得到该分类下的所有子分类的id号数组，$parentid表示某个分类id下的所有子分类,$array这个是整个数组*/
    function get_list_id_array_category($array,$parentid=0,$str='') 
	{
         $temp_str =  $this->get_list_id_category($array,$parentid,$str);
		 $temp_str = $temp_str.','.$parentid;		 
		 return del_empty_var(array_unique(explode(',', $temp_str)));		      
    }


    function html_format_category($array,$value,$parentid=0,$level=0,$ownid=0) 
	{
	       $newarray = $this->get_list_category($array,$parentid=0,$level=0,$add=1);
            $str="";
	       foreach($newarray as $v) 
		   {
		            $selected = '';
		            if($value == $v['cat_id']) 
					{
					 $selected = 'selected';
					}
					if($ownid && $ownid == $v['cat_id'])
			        {
					$str .= "<optgroup label='".$v['str_pad'].$v['cat_name']."'></optgroup>";
					}else
			       {
		            $str .= "<option value='".$v['cat_id']."' ".$selected.">".$v['str_pad'].$v['cat_name']."</option>";
				   }
	       }
	       return $str;
    }




}

?>